package com.ple.app.user;

import android.content.Context;
import android.graphics.Color;
import android.view.MotionEvent;

import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.MapOnTouchListener;
import com.esri.android.map.MapView;
import com.esri.core.geometry.MultiPath;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.Polygon;
import com.esri.core.geometry.Polyline;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.SimpleFillSymbol;
import com.esri.core.symbol.SimpleLineSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol;

/**
 * Created by John on 2016/12/6.
 * Draw point/polyline/polygon
 * Need Context, MapView, GraphicsLayer,set the geometryType
 * 优点：绘图方式较为灵活，
 * 缺点：绘制时看不到绘制轨迹，
 * 待完善
 * 目标：绘制边界---使用单点的方式，依次构建线和面。其他功能：结束，保存（保存到数据库服务器），清除图层;选择绘制的类别。
 */

public class DrawGeometryHelper extends MapOnTouchListener {
    private MultiPath path;
    private String type = "";
    private Point startPoint;

    private MapView mapView;
    private GraphicsLayer graphicsLayer;

    public DrawGeometryHelper(Context context, MapView view, GraphicsLayer graphicsLayer) {
        super(context, view);
        this.mapView = view;
        this.graphicsLayer = graphicsLayer;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }

    /**
     * Invoked when use single taps on map view ...
     * @param e // touch point
     * @return //
     */
    @Override
    public boolean onSingleTap(MotionEvent e) {
        if(type.length()>1 && type.equalsIgnoreCase("POINT")){
            graphicsLayer.removeAll();
            Graphic graphic = new Graphic(mapView.toMapPoint(new Point(e.getX(), e.getY())),
                    new SimpleMarkerSymbol(Color.RED,25, SimpleMarkerSymbol.STYLE.CIRCLE));
            //graphic.setGeometry();
            graphicsLayer.addGraphic(graphic);

            return true;
        }
        return false;
    }

    /**
     * polyline
     * @param from
     * @param to
     * @return
     */
    @Override
    public boolean onDragPointerMove(MotionEvent from, MotionEvent to) {
        if (type.length()>1&&type.equalsIgnoreCase("POLYLINE")||
                type.equalsIgnoreCase("POLYGON")){
            Point mapPt = mapView.toMapPoint(to.getX(), to.getY());

            if (startPoint == null) {
                graphicsLayer.removeAll();
                path = type.equalsIgnoreCase("POLYLINE") ? new Polyline():new Polygon();
                startPoint = mapView.toMapPoint(from.getX(),from.getY());
                path.startPath(startPoint.getX(),startPoint.getY());

                Graphic graphicLine = new Graphic(startPoint,new SimpleLineSymbol(Color.RED,5));
                graphicsLayer.addGraphic(graphicLine);
            }
            path.lineTo((float) mapPt.getX(), (float) mapPt.getY());

            return true;
        }
        return super.onDragPointerMove(from, to);
    }

    /**
     * polygon
     * @param from
     * @param to
     * @return
     */
    @Override
    public boolean onDragPointerUp(MotionEvent from, MotionEvent to) {
        if (type.length()>1&&type.equalsIgnoreCase("POLYLINE")||
                type.equalsIgnoreCase("POLYGON")){
            /*
             * When user release finger , add the last point to polyline
             */
            if (type.equalsIgnoreCase("POLYGON")) {
                path.lineTo(startPoint.getX(),startPoint.getY());
                graphicsLayer.removeAll();
                graphicsLayer.addGraphic(new Graphic(path,new SimpleFillSymbol(Color.GRAY)));
            }
            startPoint = null;
            graphicsLayer.addGraphic(new Graphic(path,new SimpleLineSymbol(Color.BLUE,3)));
            return true;
        }
        return super.onDragPointerUp(from, to);
    }

    @Override
    public void onLongPress(MotionEvent point) {
        super.onLongPress(point);
    }
}
